package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.StudentDataBean;
import com.fancy.learncenter.bean.TeacherStutasDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;


public class CartoonQueryTeacherActivity extends BaseActivity {
    public static String FLAG_KEY = "FLAG_KEY";

    ArrayList<String> itemDatas;

    @Bind(R.id.add_bt)
    Button addBt;
    @Bind(R.id.content)
    EditText content;

    String flagKey = "";
    @Bind(R.id.name_tv)
    EditText nameTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_query_teacher, Status.STATUS);
        ButterKnife.bind(this);

        classId = getIntent().getStringExtra(CartoonTeacherClassDetailActivity.CLASS_ID_KEY);
        flagKey = getIntent().getStringExtra(FLAG_KEY);
        if (TextUtils.isEmpty(flagKey)) {
            initToolbar("添加学生");
        } else {
            initToolbar("添加老师");
        }

    }

    String classId;

    @OnClick(R.id.add_bt)
    public void onViewClicked() {

        if (TextUtils.isEmpty(flagKey)) {
            queryStudent();
        } else {
            if (!TextUtils.isEmpty(nameTv.getText().toString())) {
                queryTeacher();
            } else {
                ToastUtil.show("请输入姓名");
            }

        }
    }

    private void queryTeacher() {

        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("mobile", content.getText().toString());

        HttpMehtod.getInstance().queryTeacher(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<TeacherStutasDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), TeacherStutasDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<TeacherStutasDataBean>() {
                    @Override
                    public void success(TeacherStutasDataBean dataBean) {
                        Intent intent = new Intent(CartoonQueryTeacherActivity.this, CartoonAddTeacherActivity.class);

                        intent.putExtra(CartoonAddTeacherActivity.ROLE_KEY, "1");
                        intent.putExtra(CartoonAddTeacherActivity.ROLE_DATA_KEY, dataBean.getTeacher());
                        intent.putExtra(CartoonTeacherClassDetailActivity.CLASS_ID_KEY, classId);
                        intent.putExtra("nameStr", nameTv.getText().toString());
                        startActivity(intent);
                        
                        finish();
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }

    private void queryStudent() {

        Map<String, Object> map = new HashMap<>();
        map.put("classId", classId);
        map.put("mobile", content.getText().toString());

        HttpMehtod.getInstance().queryStudent(map, new CustomNetSubscriber(this, true) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<StudentDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), StudentDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<StudentDataBean>() {
                    @Override
                    public void success(StudentDataBean dataBean) {

                        Intent intent = new Intent(CartoonQueryTeacherActivity.this, CartoonAddTeacherActivity.class);

                        if (TextUtils.isEmpty(dataBean.getStudent().getName()) && !TextUtils.isEmpty(nameTv.getText().toString())) {
                            dataBean.getStudent().setName(nameTv.getText().toString());
                        }
                        intent.putExtra(CartoonAddTeacherActivity.ROLE_KEY, "2");
                        intent.putExtra(CartoonAddTeacherActivity.ROLE_DATA_KEY, dataBean);
                        intent.putExtra(CartoonTeacherClassDetailActivity.CLASS_ID_KEY, classId);
                        startActivity(intent);

                        finish();
                    }

                    @Override
                    public void fail(String errorCode) {


                    }
                }).dealData();
            }

        });
    }
}
